var request = require('request');
var _ = require('underscore');
//var highcharts = require('node-highcharts');

/*
 * GET home page.
 */

exports.index = function(req, res){
  res.render('index', { title: 'Express' });
};

exports.segmentHistory = function(req, res) {

/*
  var data = {"efforts":[{"id":7973743,"activityId":526735,"startDate":"2008-08-23T18:46:03Z","startDateLocal":"2008-08-23T11:46:03Z","timeZoneOffset":-28800,"elapsedTime":556,"athlete":{"id":10749,"name":"Doug Read","username":"dreadjr"}},{"id":7973811,"activityId":526738,"startDate":"2008-08-28T23:44:16Z","startDateLocal":"2008-08-28T16:44:16Z","timeZoneOffset":-28800,"elapsedTime":537,"athlete":{"id":10749,"name":"Doug Read","username":"dreadjr"}},{"id":7973858,"activityId":526739,"startDate":"2008-08-30T20:32:58Z","startDateLocal":"2008-08-30T13:32:58Z","timeZoneOffset":-28800,"elapsedTime":537,"athlete":{"id":10749,"name":"Doug Read","username":"dreadjr"}},{"id":7973883,"activityId":526740,"startDate":"2008-09-02T23:41:23Z","startDateLocal":"2008-09-02T16:41:23Z","timeZoneOffset":-28800,"elapsedTime":513,"athlete":{"id":10749,"name":"Doug Read","username":"dreadjr"}},{"id":7972781,"activityId":526715,"startDate":"2009-07-11T17:55:14Z","startDateLocal":"2009-07-11T10:55:14Z","timeZoneOffset":-28800,"elapsedTime":587,"athlete":{"id":10749,"name":"Doug Read","username":"dreadjr"}},{"id":7972507,"activityId":526685,"startDate":"2009-08-02T21:48:44Z","startDateLocal":"2009-08-02T14:48:44Z","timeZoneOffset":-28800,"elapsedTime":581,"athlete":{"id":10749,"name":"Doug Read","username":"dreadjr"}},{"id":7953541,"activityId":525062,"startDate":"2010-05-20T23:23:27Z","startDateLocal":"2010-05-20T16:23:27Z","timeZoneOffset":-28800,"elapsedTime":646,"athlete":{"id":10749,"name":"Doug Read","username":"dreadjr"}},{"id":7970710,"activityId":526566,"startDate":"2011-05-07T19:51:17Z","startDateLocal":"2011-05-07T12:51:17Z","timeZoneOffset":-28800,"elapsedTime":785,"athlete":{"id":10749,"name":"Doug Read","username":"dreadjr"}},{"id":8927692,"activityId":598985,"startDate":"2011-05-21T18:21:15Z","startDateLocal":"2011-05-21T11:21:15Z","timeZoneOffset":-28800,"elapsedTime":604,"athlete":{"id":10749,"name":"Doug Read","username":"dreadjr"}},{"id":267094691,"activityId":14247932,"startDate":"2012-07-19T23:46:28Z","startDateLocal":"2012-07-19T16:46:28Z","timeZoneOffset":-28800,"elapsedTime":571,"athlete":{"id":10749,"name":"Doug Read","username":"dreadjr"}},{"id":312910011,"activityId":16975133,"startDate":"2012-08-06T22:54:32Z","startDateLocal":"2012-08-06T15:54:32Z","timeZoneOffset":-28800,"elapsedTime":529,"athlete":{"id":10749,"name":"Doug Read","username":"dreadjr"}}],"segment":{"id":633807,"name":"Holmes Point Drive North (Seminary Hill)"}};
  console.log(_.map(data.efforts, function(effort) {
    return effort.startDateLocal;
  }));
  res.render('segmentHistory', { json: JSON.stringify(data) });
*/
  // maybe only take the 5 fastest times, no need to graph all of them.????
  // 1665029
  // 622643
  request('http://app.strava.com/api/v1/segments/1665029/efforts?athleteId=10749', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      res.render('segmentHistory', { json: body });
    }
  });

};

// use socket.io